import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/tabbar/gt_thirdly_tabbar_v3.dart';
import 'package:lib_uikit/widgets/tabbar/underline/gt_tabs.dart';

/// @Title   Secondary类型的tab
///   文字大小16/14，tab间隔8，tab高度31
/// @Author: bryce
/// @CreateDate: 2023年10月11日20:21:34
class GTSecondaryTabBarV3 extends StatelessWidget {
  final List<String>? tabTitles;
  final List<GTTabModalV3>? tabData;
  final TabController? controller;
  final EdgeInsetsGeometry? padding;
  final ValueChanged<int>? onTap;
  final double? tabTextSize;
  final double tabHeight;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;
  final bool? showDivider;
  final CrossAxisAlignment? crossAxisAlignment;

  const GTSecondaryTabBarV3(
      {super.key,
      this.tabTitles,
      this.tabData,
      this.controller,
      this.padding,
      this.onInterceptSwitch,
      this.onInterceptTapSwitch,
      this.onTap,
      this.tabTextSize = 16,
      this.showDivider = false,
      this.crossAxisAlignment,
      this.tabHeight = 32});

  @override
  Widget build(BuildContext context) {
    Theme.of(context);
    TextStyle labelStyle = gtTextStyleW500(fontSize: tabTextSize);
    // 小括号style
    TextStyle bracketsStyle = gtTextStyleW500(fontSize: 14);
    return Column(
      crossAxisAlignment: crossAxisAlignment ?? CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        GTTabBar(
            dividerColor: Colors.transparent,
            controller: controller,
            isScrollable: true,
            padding: padding,
            unselectedLabelColor: GTColorsV3.uikitText3V3.color,
            labelColor: GTColorsV3.uikitText1V3.color,
            labelPadding: GTEdgeInsetsOnly(left: 0, right: 16),
            labelStyle: gtTextStyleW500(fontSize: tabTextSize).copyWith(height: 1),
            unselectedLabelStyle: gtTextStyleW500(fontSize: tabTextSize).copyWith(height: 1),
            onTap: onTap,
            onInterceptSwitch: onInterceptSwitch,
            onInterceptTapSwitch: onInterceptTapSwitch,
            overlayColor: MaterialStateProperty.resolveWith<Color?>(
              (Set<MaterialState> states) => Colors.transparent,
            ),
            indicator: GTUnderlineTabIndicator(
                context: context,
                tabTitles: tabTitles,
                tabData: tabData,
                labelStyle: labelStyle,
                bracketsStyle: bracketsStyle,
                borderSide: BorderSide(width: 2, color: GTColorsV3.uikitText1V3.color)),
            tabs: tabs()),
        if (showDivider ?? false) ...[
          Container(
            height: 1,
            color: GTColorsV3.divider2.color,
          ),
        ]
      ],
    );
  }

  List<Widget> tabs() {
    if (tabTitles != null && tabTitles?.isNotEmpty == true) {
      return tabTitles!.map((e) => _tabItem(e)).toList();
    } else if (tabData != null && tabData?.isNotEmpty == true) {
      return tabData!.map((e) => _tabDataItem(e)).toList();
    } else {
      return [];
    }
  }

  Widget _tabItem(String title) {
    return GTTab(
      height: tabHeight,
      child: Text(title),
    );
  }

  Widget _tabDataItem(GTTabModalV3 bean) {
    return GTTab(
      height: tabHeight,
      key: bean.widgetKey,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (bean.leftIcon != null) ...[
            bean.leftIcon!,
            SizedBox(width: bean.iconPadding),
          ],
          Text(bean.text),
          if (bean.rightIcon != null) ...[
            SizedBox(width: bean.iconPadding),
            bean.rightIcon!,
          ],
        ],
      ),
    );
  }
}
